#!/bin/sh
#
#
# @author Luigi Lorenzo Arpino [luigiarpino@yahoo.it]
#
#

# -------  CONFIGURATION SECTION ---------

# Please set the correct path to command "losetup"
LOSETUP_PATH=/sbin
# Please set the correct path to command "kpartx"
KPARTX_PATH=/sbin

# ---- END OF CONFIGURATION SECTION ------ 


# ----- PLEASE DO NOT MODIFY THE TEXT BELOW THIS LINE -----
# Check path to command "losetup"
if [ ! -f "$LOSETUP_PATH/losetup" ]; then
	echo "ERROR [$0]: Undefined command $LOSETUP_PATH/losetup"
	exit 1
fi
# Check path to command "kpartx"
if [ ! -f "$KPARTX_PATH/kpartx" ]; then
	echo "ERROR [$0]: Undefined command $KPARTX_PATH/kpartx"
	exit 1
fi
# Check number of parameter

if [ "$#" != 8  ];then
	echo "ERROR $0: Incorrect number of parameter!" 
	exit 1
fi


DISTRIBUTION=$1
SOURCE=$2
DESTINATION=$3
IP=$4
NETMASK=$5
GATEWAY=$6
NIC=$7
MAC=$8


echo "DISTRIBUTION $DISTRIBUTION"
if [  -f $DESTINATION ];then
        echo "Virtual Machine $DESTINATION  exists!"
else
        # Copy the image
		echo "Duplicating image file..."
		#if ! cp "$SOURCE" "$DESTINATION"
		if ! nice -n 19 cp "$SOURCE" "$DESTINATION"
		then
			echo "[ERROR] $0: Unable to copy $SOURCE to $DESTINATION"
			exit 1
		fi
fi


echo "[OK]"

chmod 777 $DESTINATION

# Mounting file system
echo "Mounting file system..."
$LOSETUP_PATH/losetup /dev/loop0 $DESTINATION
$KPARTX_PATH/kpartx -va /dev/loop0
mkdir /tmp/tmpclone
mount /dev/mapper/loop0p1 /tmp/tmpclone
#mount /dev/mapper/loop0p2 mnt/  -- Non necessario

if [ "$DISTRIBUTION" != debian ] && [ "$DISTRIBUTION" != centos ] && [ "$DISTRIBUTION" != fedora ];then
	echo "ERROR [$0]: Distrubution $DISTRIBUTION not yet supported"
	exit 1
fi

echo "Configuring network interface of $DISTRIBUTION distribution!"

if [ "$DISTRIBUTION" = debian ];then

	#Configuring network interface
	rm /tmp/tmpclone/etc/network/interfaces
	echo "# This file describes the network interfaces" >> /tmp/tmpclone/etc/network/interfaces
	echo "# The loopback network interface" >> /tmp/tmpclone/etc/network/interfaces
	echo "auto lo" >> /tmp/tmpclone/etc/network/interfaces
	echo "iface lo inet loopback" >> /tmp/tmpclone/etc/network/interfaces
	echo "# The primary network interface" >> /tmp/tmpclone/etc/network/interfaces
	echo "auto $NIC" >> /tmp/tmpclone/etc/network/interfaces
	echo "iface $NIC inet static" >> /tmp/tmpclone/etc/network/interfaces
	echo "address $IP" >> /tmp/tmpclone/etc/network/interfaces
	echo "   netmask $NETMASK" >> /tmp/tmpclone/etc/network/interfaces
	#echo "   network 192.168.0.0" >> /tmp/tmpclone/etc/network/interfaces
	#echo "   broadcast 192.168.0.255" >> /tmp/tmpclone/etc/network/interfaces
	echo "   gateway $GATEWAY" >> /tmp/tmpclone/etc/network/interfaces
	echo "   hwaddress ether $MAC" >> /tmp/tmpclone/etc/network/interfaces
fi

if [ "$DISTRIBUTION" = centos ];then

	#Configuring network interface
	rm  /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "DEVICE=$NIC" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "IPADDR=$IP" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0 
	echo "NETMASK=$NETMASK" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "BOOTPROTO=static" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "ONBOOT=yes" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "HWADDR=$MAC" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "GATEWAY=$GATEWAY" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
fi

if [ "$DISTRIBUTION" = fedora ];then

	#Configuring network interface
	rm  /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "DEVICE=$NIC" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "IPADDR=$IP" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0 
	echo "NETMASK=$NETMASK" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "BOOTPROTO=static" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "ONBOOT=yes" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "HWADDR=$MAC" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
	echo "GATEWAY=$GATEWAY" >> /tmp/tmpclone/etc/sysconfig/network-scripts/ifcfg-eth0
fi

#unmounting file system
echo "Unmounting file stystem..."
umount /tmp/tmpclone
$KPARTX_PATH/kpartx -d /dev/loop0
$LOSETUP_PATH/losetup -d /dev/loop0
rm -r /tmp/tmpclone



